<template>
  <el-dialog
    :title="!dataForm.opId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="opUserId">
      <el-input v-model="dataForm.opUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="动态数量" prop="opDynamicCondition">
      <el-input v-model="dataForm.opDynamicCondition" placeholder="动态数量"></el-input>
    </el-form-item>
    <el-form-item label="评论数量" prop="opComment">
      <el-input v-model="dataForm.opComment" placeholder="评论数量"></el-input>
    </el-form-item>
    <el-form-item label="话题数量" prop="opTopic">
      <el-input v-model="dataForm.opTopic" placeholder="话题数量"></el-input>
    </el-form-item>
    <el-form-item label="帖子数量" prop="opInvitation">
      <el-input v-model="dataForm.opInvitation" placeholder="帖子数量"></el-input>
    </el-form-item>
    <el-form-item label="视频id" prop="opVideoId">
      <el-input v-model="dataForm.opVideoId" placeholder="视频id"></el-input>
    </el-form-item>
    <el-form-item label="书单数量" prop="opBookList">
      <el-input v-model="dataForm.opBookList" placeholder="书单数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          opId: 0,
          opUserId: '',
          opDynamicCondition: '',
          opComment: '',
          opTopic: '',
          opInvitation: '',
          opVideoId: '',
          opBookList: ''
        },
        dataRule: {
          opUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          opDynamicCondition: [
            { required: true, message: '动态数量不能为空', trigger: 'blur' }
          ],
          opComment: [
            { required: true, message: '评论数量不能为空', trigger: 'blur' }
          ],
          opTopic: [
            { required: true, message: '话题数量不能为空', trigger: 'blur' }
          ],
          opInvitation: [
            { required: true, message: '帖子数量不能为空', trigger: 'blur' }
          ],
          opVideoId: [
            { required: true, message: '视频id不能为空', trigger: 'blur' }
          ],
          opBookList: [
            { required: true, message: '书单数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.opId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.opId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tboperation/info/${this.dataForm.opId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.opUserId = data.tbOperation.opUserId
                this.dataForm.opDynamicCondition = data.tbOperation.opDynamicCondition
                this.dataForm.opComment = data.tbOperation.opComment
                this.dataForm.opTopic = data.tbOperation.opTopic
                this.dataForm.opInvitation = data.tbOperation.opInvitation
                this.dataForm.opVideoId = data.tbOperation.opVideoId
                this.dataForm.opBookList = data.tbOperation.opBookList
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tboperation/${!this.dataForm.opId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'opId': this.dataForm.opId || undefined,
                'opUserId': this.dataForm.opUserId,
                'opDynamicCondition': this.dataForm.opDynamicCondition,
                'opComment': this.dataForm.opComment,
                'opTopic': this.dataForm.opTopic,
                'opInvitation': this.dataForm.opInvitation,
                'opVideoId': this.dataForm.opVideoId,
                'opBookList': this.dataForm.opBookList
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
